class EventRouter {
  #api:Record<string, any> = {}
  routers
  constructor() {
    this.routers = []
  }
  //添加api
  addApi(key:string, api:any): void {
    this.#api[key] = api
  }
  // 获取api
  getApi<T = any>(key: string): T {
    return this.#api[key]
  }
  //添加多个路由
  addRouters(routers): void {
    this.routers = this.routers.concat(routers)
  }
  //触发指定名称的路由回调
  router(data): void {
    // 遍历routers中的所有事件，匹配到name相同的事件，然后执行callback
    for (let i = 0; i < this.routers.length; i++) {
      const r = this.routers[i]
      if (data.name == r.name && r.callback) {
        return r.callback(this.#api, data)
      }
    }
  }
}

export default new EventRouter()